home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Devel / SelfStubber.pm
Encoding:
Perl POD Document  |  1999-12-28  |  4.2 KB  |  133 lines

  1. package Devel::SelfStubber;
  2. require SelfLoader;
  3. @ISA = qw(SelfLoader);
  4. @EXPORT = 'AUTOLOAD';
  5. $JUST_STUBS = 1;
  6. $VERSION = 1.01; sub Version {$VERSION}
  7.  
  8.  
  9. sub _add_to_cache {
  10.     my($self,$fullname,$pack,$lines, $prototype) = @_;
  11.     push(@DATA,@{$lines});
  12.     if($fullname){push(@STUBS,"sub $fullname $prototype;\n")}; # stubs
  13.     '1;';
  14. }
  15.  
  16. sub _package_defined {
  17.     my($self,$line) = @_;
  18.     push(@DATA,$line);
  19. }
  20.  
  21. sub stub {
  22.     my($self,$module,$lib) = @_;
  23.     my($line,$end,$fh,$mod_file,$found_selfloader);
  24.     $lib ||= '.';
  25.     ($mod_file = $module) =~ s,::,/,g;
  26.     
  27.     $mod_file = "$lib/$mod_file.pm";
  28.     $fh = "${module}::DATA";
  29.  
  30.     open($fh,$mod_file) || die "Unable to open $mod_file";
  31.     while(defined ($line = <$fh>) and $line !~ m/^__DATA__/) {
  32.     push(@BEFORE_DATA,$line);
  33.     $line =~ /use\s+SelfLoader/ && $found_selfloader++;
  34.     }
  35.     $line =~ m/^__DATA__/ || die "$mod_file doesn't contain a __DATA__ token";
  36.     $found_selfloader || 
  37.     print 'die "\'use SelfLoader;\' statement NOT FOUND!!\n"',"\n";
  38.     $self->_load_stubs($module);
  39.     if ( fileno($fh) ) {
  40.     $end = 1;
  41.     while(defined($line = <$fh>)) {
  42.         push(@AFTER_DATA,$line);
  43.     }
  44.     }
  45.     unless ($JUST_STUBS) {
  46.         print @BEFORE_DATA;
  47.     }
  48.     print @STUBS;
  49.     unless ($JUST_STUBS) {
  50.         print "1;\n__DATA__\n",@DATA;
  51.         if($end) { print "__END__\n",@AFTER_DATA; }
  52.     }
  53. }
  54.  
  55. 1;
  56. __END__
  57.  
  58. =head1 NAME
  59.  
  60. Devel::SelfStubber - generate stubs for a SelfLoading module
  61.  
  62. =head1 SYNOPSIS
  63.  
  64. To generate just the stubs:
  65.  
  66.     use Devel::SelfStubber;
  67.     Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');
  68.  
  69. or to generate the whole module with stubs inserted correctly
  70.  
  71.     use Devel::SelfStubber;
  72.     $Devel::SelfStubber::JUST_STUBS=0;
  73.     Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');
  74.  
  75. MODULENAME is the Perl module name, e.g. Devel::SelfStubber,
  76. NOT 'Devel/SelfStubber' or 'Devel/SelfStubber.pm'.
  77.  
  78. MY_LIB_DIR defaults to '.' if not present.
  79.  
  80. =head1 DESCRIPTION
  81.  
  82. Devel::SelfStubber prints the stubs you need to put in the module
  83. before the __DATA__ token (or you can get it to print the entire
  84. module with stubs correctly placed). The stubs ensure that if
  85. a method is called, it will get loaded. They are needed specifically
  86. for inherited autoloaded methods.
  87.  
  88. This is best explained using the following example:
  89.  
  90. Assume four classes, A,B,C & D.
  91.  
  92. A is the root class, B is a subclass of A, C is a subclass of B,
  93. and D is another subclass of A.
  94.  
  95.                         A
  96.                        / \
  97.                       B   D
  98.                      /
  99.                     C
  100.  
  101. If D calls an autoloaded method 'foo' which is defined in class A,
  102. then the method is loaded into class A, then executed. If C then
  103. calls method 'foo', and that method was reimplemented in class
  104. B, but set to be autoloaded, then the lookup mechanism never gets to
  105. the AUTOLOAD mechanism in B because it first finds the method
  106. already loaded in A, and so erroneously uses that. If the method
  107. foo had been stubbed in B, then the lookup mechanism would have
  108. found the stub, and correctly loaded and used the sub from B.
  109.  
  110. So, for classes and subclasses to have inheritance correctly
  111. work with autoloading, you need to ensure stubs are loaded.
  112.  
  113. The SelfLoader can load stubs automatically at module initialization
  114. with the statement 'SelfLoader-E<gt>load_stubs()';, but you may wish to
  115. avoid having the stub loading overhead associated with your
  116. initialization (though note that the SelfLoader::load_stubs method
  117. will be called sooner or later - at latest when the first sub
  118. is being autoloaded). In this case, you can put the sub stubs
  119. before the __DATA__ token. This can be done manually, but this
  120. module allows automatic generation of the stubs.
  121.  
  122. By default it just prints the stubs, but you can set the
  123. global $Devel::SelfStubber::JUST_STUBS to 0 and it will
  124. print out the entire module with the stubs positioned correctly.
  125.  
  126. At the very least, this is useful to see what the SelfLoader
  127. thinks are stubs - in order to ensure future versions of the
  128. SelfStubber remain in step with the SelfLoader, the
  129. SelfStubber actually uses the SelfLoader to determine which
  130. stubs are needed.
  131.  
  132. =cut
  133.